﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BugZillaCrawler.UI
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        Queue<string> _status = new Queue<string>();

        void UpdateDescription()
        {
            var bugs = DataAccess.Database.Bugs.Where(b => b.Description.Length == 0);
            foreach (var bug in bugs)
            {
                if (bug.Description.Length == 0)
                {
                    string description = Crawler.DownloadBugDescription(bug.Id);

                    if (!string.IsNullOrEmpty(description))
                    {
                        bug.Description = description;

                        _status.Enqueue("Updating bug: " + bug.Id);

                        DataAccess.Database.SubmitChanges();
                    }
                }
            }
        }

        private void updateStatusTimer_Tick(object sender, EventArgs e)
        {
            while (_status.Count > 0)
                lstStatus.Items.Add(_status.Dequeue());
        }

        private void btnUpdateDescription_Click(object sender, EventArgs e)
        {
            btnUpdateDescription.Enabled = false;
            updateDescriptionWorker.RunWorkerAsync();
        }

        private void updateDescriptionWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            UpdateDescription();
        }

        private void updateDescriptionWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnUpdateDescription.Enabled = true;
        }

        private void btnUpdateBugCreationTime_Click(object sender, EventArgs e)
        {
            btnUpdateBugCreationTime.Enabled = false;
            updateBugCreationDateWorker.RunWorkerAsync();
        }

        private void UpdateBugCreationTime()
        {
            var bugs = DataAccess.Database.Bugs;
            foreach (var bug in bugs)
            {
                DateTime time = Crawler.DownloadBugCreatingTime(bug.Id);

                bug.Time = time;
                _status.Enqueue("Updating bug: " + bug.Id);
                DataAccess.Database.SubmitChanges();

            }
        }

        private void updateBugCreationDateWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            UpdateBugCreationTime();
        }

        private void updateBugCreationDateWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnUpdateBugCreationTime.Enabled = true;
        }

        private void btnNoOfRelatedBugs_Click(object sender, EventArgs e)
        {
            int count = 0;
            List<string> existingIds = new List<string>();
            var bugs = DataAccess.Database.Bugs.Where(t => t.ReviewStatus == "DONE+");
            foreach (var item in bugs)
            {
                if (Crawler.IsThisBugRelatedToOtherBug(item.Id, existingIds))
                    count++;
            }

            MessageBox.Show(count.ToString() + "|" + existingIds.ToString());
        }
    }
}
